package lily.phs.urh;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import lily.phs.database.Constants;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

public class RecordCover extends LinearLayout implements OnClickListener, OnFocusChangeListener,
		Constants {
	
	private Context context;
	private EditText et_name,et_address,et_household,et_tel,et_rural,et_village,
	et_recorder_unit,et_recorder,et_doctor,et_date;
	private Spinner sp_address_team,sp_hh_team;

	private String name,address,household,tel,rural,village,recorder_unit,recorder,doctor,date;
	
	public RecordCover(Context context){
		super(context);
		this.context = context;
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.build_new_profile, null);
		
		et_name=(EditText)view.findViewById(R.id.et_jmjkda_name);
        et_address=(EditText)view.findViewById(R.id.et_jmjkda_nowplace);
        et_household=(EditText)view.findViewById(R.id.et_jmjkda_hjdz);
        et_tel=(EditText)view.findViewById(R.id.et_jmjkda_tel);
        et_rural=(EditText)view.findViewById(R.id.et_jmjkda_xzname);
        et_village=(EditText)view.findViewById(R.id.et_jmjkda_cwhname);
        et_recorder_unit=(EditText)view.findViewById(R.id.et_jmjkda_jddw);
        et_recorder=(EditText)view.findViewById(R.id.et_jmjkda_jdr);
        et_doctor=(EditText)view.findViewById(R.id.et_jmjkda_doctor);
        et_date=(EditText)view.findViewById(R.id.et_jmjkda_date);
        et_date.setInputType(InputType.TYPE_NULL);
        et_date.setOnClickListener(this);
        et_date.setOnFocusChangeListener(this);
        sp_address_team = (Spinner)view.findViewById(R.id.address_team);
        sp_hh_team = (Spinner)view.findViewById(R.id.hh_team);
		LinearLayout ll_bottom = (LinearLayout)view.findViewById(R.id.bottom_control);
		ll_bottom.setVisibility(View.GONE);
         
        //宁夏回族自治区吴忠市青铜峡市峡口镇赵渠村
		SharedPreferences ui = context.getSharedPreferences(USERINFO, Context.MODE_PRIVATE);
		
        String province = context.getResources().getString(R.string.def_province);
        String city = context.getResources().getString(R.string.def_city);
        String county = context.getResources().getString(R.string.def_county);
        String rural = context.getResources().getString(R.string.def_rural);
        String village = ui.getString("def_village","");
        String operator = ui.getString("operator_name","");
        
        et_address.setText(province+city+county+rural+village);
        et_household.setText(province+city+county+rural+village);
        et_rural.setText(rural);
        et_village.setText(village+"村委会");
		et_recorder_unit.setText(rural+village);
		et_recorder.setText(operator);
		et_doctor.setText(operator);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date(System.currentTimeMillis());
		et_date.setText(sf.format(date));
		
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
		this.addView(view, lp);
	}

	public String getName() {
		name = et_name.getText().toString();
		return name;
	}

	public String getAddress() {
		address = et_address.getText().toString()+sp_address_team.getSelectedItem().toString();
		return address;
	}

	public String getHousehold() {
		household = et_household.getText().toString()+sp_hh_team.getSelectedItem().toString();
		return household;
	}

	public String getTel() {
		tel = et_tel.getText().toString();
		return tel;
	}

	public String getRural() {
		rural = et_rural.getText().toString();
		return rural;
	}

	public String getVillage() {
		village = et_village.getText().toString();
		return village;
	}

	public String getRecorder_unit() {
		recorder_unit = et_recorder_unit.getText().toString();
		return recorder_unit;
	}

	public String getRecorder() {
		recorder = et_recorder.getText().toString();
		return recorder;
	}

	public String getDoctor() {
		doctor = et_doctor.getText().toString();
		return doctor;
	}

	public String getDate() {
		date = et_date.getText().toString();
		return date;
	}

	/**
	 * 检查信息的完整性，必填项不能为空
	 * @return true 完整，false 否之
	 */
	public boolean checkIntegrity() {
		// TODO Auto-generated method stub
		name = et_name.getText().toString();
		address = et_address.getText().toString()+sp_address_team.getSelectedItem().toString();
		household = et_household.getText().toString()+sp_hh_team.getSelectedItem().toString();
		tel = et_tel.getText().toString();
		rural = et_rural.getText().toString();
		village = et_village.getText().toString();
		recorder_unit = et_recorder_unit.getText().toString();
		recorder = et_recorder.getText().toString();
		doctor = et_doctor.getText().toString();
		date = et_date.getText().toString();
		if(name==null||name.length()==0){
			Toast.makeText(context, "请填写姓名", Toast.LENGTH_LONG).show();
			return false;
		}
		if(address==null||address.length()==0){
			Toast.makeText(context, "请填写现住址", Toast.LENGTH_LONG).show();
			return false;
		}
		if(household==null||household.length()==0){
			Toast.makeText(context, "请填写户籍地址", Toast.LENGTH_LONG).show();
			return false;
		}
		if(tel==null||tel.length()==0){
			Toast.makeText(context, "请填写联系电话", Toast.LENGTH_LONG).show();
			return false;
		}
		if(rural==null||rural.length()==0){
			Toast.makeText(context, "请填写乡镇（街道）名称", Toast.LENGTH_LONG).show();
			return false;
		}
		if(village==null||village.length()==0){
			Toast.makeText(context, "请填写村（居）委会名称", Toast.LENGTH_LONG).show();
			return false;
		}
		if(recorder_unit==null||recorder_unit.length()==0){
			Toast.makeText(context, "请填写建档单位", Toast.LENGTH_LONG).show();
			return false;
		}
		if(recorder==null||recorder.length()==0){
			Toast.makeText(context, "请填写建档人", Toast.LENGTH_LONG).show();
			return false;
		}
		if(doctor==null||doctor.length()==0){
			Toast.makeText(context, "请填写责任医生", Toast.LENGTH_LONG).show();
			return false;
		}
		if(date==null||date.length()==0){
			Toast.makeText(context, "请填写建档日期", Toast.LENGTH_LONG).show();
			return false;
		}
		
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==et_date){
			showDateDialog(et_date);
		}
		
	}
	
	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		// TODO Auto-generated method stub
		if(v==et_date&&hasFocus){
			showDateDialog(et_date);
		}
	}
	
	/**
     * 系统自带的日期选择控件，因为自定义的需要3.0以上的版本
     * ！！仅当前测试使用！！
     * @param vdate 日期选择的触发控件
     */
    private void showDateDialog(EditText v) {
		// TODO Auto-generated method stub
    	Calendar cal = Calendar.getInstance();
    	int def_year = cal.get(Calendar.YEAR);
    	int def_month = cal.get(Calendar.MONTH);
    	int def_day = cal.get(Calendar.DAY_OF_MONTH);
    	final EditText vdate = v;
    	
    	new DatePickerDialog(context, 
    			new DatePickerDialog.OnDateSetListener() {
					
					@Override
					public void onDateSet(DatePicker view, int year, int monthOfYear,
							int dayOfMonth) {
						// TODO Auto-generated method stub
				    	Calendar c = Calendar.getInstance();
						c.set(Calendar.YEAR, year);
						c.set(Calendar.MONTH, monthOfYear);
						c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
						SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
						vdate.setText(sf.format(c.getTime()));
						System.out.println(vdate.getText().toString());
					}
				}, 
				def_year, def_month, def_day)//点击日期控件时默认显示当前日期
    	.show();
		
	}

	

}
